package cn.com.elite;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.softvan.search.server.imp.SearchSectionImp;
import com.softvan.tir.entity.SearchBean;

public class RoadQueryServlet extends HttpServlet{
	private static final Logger log=Logger.getLogger(RoadQueryServlet.class);
	
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		request.setCharacterEncoding("UTF-8");
		String name=request.getParameter("name");    
		log.info("name:"+name);
		name=URLDecoder.decode(name,"UTF-8");
	    log.info("name:"+name);
	    String size1=request.getParameter("size");    
	    int size=Integer.parseInt(size1);
	    log.info("size:"+size);	    
	    String returnxml="";
	    try{
	    	List<SearchBean> sourceList=SearchSectionImp.findSearchIndex(name,size);
	    	if(sourceList!=null){
	    		for(SearchBean sourceBean:sourceList){
	    			returnxml=returnxml+"<RoadInfo>";
	    			String Roadid=sourceBean.getRoadid();
	    			returnxml=returnxml+"<Roadid>"+Roadid+"</Roadid>";
	    			String City=sourceBean.getCity();
	    			returnxml=returnxml+"<City>"+City+"</City>";
	    			String District=sourceBean.getDistrict();
	    			returnxml=returnxml+"<District>"+District+"</District>";
	    			String InputRoadNumber=sourceBean.getInputRoadNumber();
	    			returnxml=returnxml+"<InputRoadNumber>"+InputRoadNumber+"</InputRoadNumber>";
	    			String Landmark=sourceBean.getLandmark();
	    			returnxml=returnxml+"<Landmark>"+Landmark+"</Landmark>";
	    			String MaxRoadNumber=sourceBean.getMaxRoadNumber();
	    			returnxml=returnxml+"<MaxRoadNumber>"+MaxRoadNumber+"</MaxRoadNumber>";
	    			String Roads=sourceBean.getRoads();
	    			returnxml=returnxml+"<Roads>"+Roads+"</Roads>";
	    			returnxml=returnxml+"</RoadInfo>";
	    		}
	    	}
	    }catch(Exception e){
	    	log.error(Util.getERRO(e));
	    	returnxml=Util.getERRO(e);
	    }
	    returnxml="<?xml version='1.0' encoding='utf-8'?><root><value>"+returnxml+"</value></root>";
		request.setCharacterEncoding("UTF-8");
	    response.setContentType("text/html;charset=utf-8;");
	    PrintWriter out=null;
		try{			
			out=response.getWriter();
		}catch(IOException e) {
			log.error(Util.getERRO(e));
		}
	    log.info("xml:"+returnxml);
	    out.print(returnxml);
	}
}
